#!/bin/bash

. /minioslib || exit 1

pkg update

# install packages
if [ -f $SCRIPT_DIR/package.list ]; then
    install_packages
fi

(cd /rootcopy-install && cp --parents -afr * /)

rm -f /usr/share/applications/chromium-flux.desktop
rm -f /usr/share/applications/compton.desktop

rm -f /etc/skel/.fluxbox/fbpotgen.sh
rm -f /etc/skel/.fluxbox/menu.pot

if [ -f /usr/share/applications/yad-icon-browser.desktop ]; then
    rm -f /usr/share/applications/yad-icon-browser.desktop
fi

if [ $LOCALE = "ru_RU" ] || [ $LOCALE = "es_ES" ] || [ $LOCALE = "pt_BR" ] || [ $MULTILINGUAL = "true" ]; then
    pkg install fbxkb
fi

# For each .desktop file in the /usr/share/applications directory
for FILE in /usr/share/applications/*.desktop; do

    # If the file contains a translated name for the current language layout
    if grep -q "Name\[$LAYOUTID\]" $FILE; then

        # Find and store the translated name, generic name, and comment
        TRANSLATED_NAME=$(grep "Name\[$LAYOUTID\]" $FILE | head -1 | cut -d= -f2-)
        TRANSLATED_GENERIC_NAME=$(grep "GenericName\[$LAYOUTID\]" $FILE | head -1 | cut -d= -f2-)
        TRANSLATED_COMMENT=$(grep "Comment\[$LAYOUTID\]" $FILE | head -1 | cut -d= -f2-)

        # If the translated name is not empty, replace the original name with it
        [ ! -z "$TRANSLATED_NAME" ] && sed -i -e "s/^Name=.*/Name=$TRANSLATED_NAME/g" $FILE

        # If the generic name is not empty, replace the original generic name with it
        [ ! -z "$TRANSLATED_GENERIC_NAME" ] && sed -i -e "s/^GenericName=.*/GenericName=$TRANSLATED_GENERIC_NAME/g" $FILE

        # If the comment is not empty, replace the original comment with it
        [ ! -z "$TRANSLATED_COMMENT" ] && sed -i -e "s/^Comment=.*/Comment=$TRANSLATED_COMMENT/g" $FILE

        # Remove the lines with translated names, generic names, and comments
        sed -i -e "/Name\[/d;/GenericName\[/d;/Comment\[/d" $FILE
    fi
done
